<?php
$page_type = "A";
require_once 'header.php';

// Get the values of the record to be inserted
$client_id = trim($_SESSION['client_id']);
$password = $_SESSION['password'];
$fname = trim($_SESSION['fname']);
$lname = trim($_SESSION['lname']);
$dob = trim($_SESSION['dob']);
$street = trim($_SESSION['street']);
$city = trim($_SESSION['city']);
$state = trim($_SESSION['state']);
$zip = trim($_SESSION['zip']);

if (!empty($_SESSION['sid'])) {
  $sid = trim($_SESSION['sid']);
}
else {
  // Get a sequence number from sequence Student_Seq
  $sql = "select Student_Seq.nextval from dual";

  $result_array = execute_sql_in_oracle($sql);
  $result = $result_array["flag"];
  $cursor = $result_array["cursor"];

  if (!$result) {
    $client_add_error = "Student ID generation failed!";
    header("Location:client_add.php?client_add_error=$client_add_error");
  }
  else if (!($values = oci_fetch_array($cursor))) {
    $client_add_error = "Student ID generation failed!";
    header("Location:client_add.php?client_add_error=$client_add_error");
  }
  else {
    oci_free_statement($cursor);
	$sid = $values[0];
	$_SESSION['sid'] = $sid;
  }
}

// Client type
if ($_SESSION['client_type'] == "A") {
  $aflag = '1';
  $sflag = '0';
}
else if ($_SESSION['client_type'] == "S") {
  $aflag = '0';
  $sflag = '1';
}
else if ($_SESSION['client_type'] == "B") {
  $aflag = '1';
  $sflag = '1';
}
else {
  $aflag = '0';
  $sflag = '0';
}  

// Student type
if ($_SESSION['student_type'] == "U") {
  $stflag = '0';
}
else if ($_SESSION['student_type'] == "G") {
  $stflag = '1';
}  
else {
  $stflag = ' ';
}

// Probation status
if ($_SESSION['input_pflag'] == "y") {
  $pflag = '1';
}
else if ($_SESSION['input_pflag'] == "n") {
  $pflag = '0';
}  
else {
  $pflag = ' ';
}

$client_type = $_SESSION['client_type'];

// Attempt to create user account only when sequence number is generated
if (isset($sid)) {
  // Get initials of user account name and combine it with 
  // auto-generated sequence number
  $fin = ucfirst(substr("$fname", 0, 1));
  $lin = ucfirst(substr("$lname", 0, 1));
  $sid = $fin.$lin.$sid;

  // Form the insertion sql string and run it
  $sql = "insert into CLIENT (Client_ID, Password, Fname, Lname, AFlag, SFlag, Sid, DOB, STFlag, PFlag, Street, City, State, Zip) 
          values ('$client_id','$password','$fname','$lname','$aflag','$sflag', '$sid', '$dob', '$stflag', '$pflag', '$street', '$city', '$state', '$zip')";

  $result_array = execute_sql_in_oracle($sql);
  $result = $result_array["flag"];
  $cursor = $result_array["cursor"];

  if (!$result) {
    $client_add_error = "New user creation failed. Try again.";
    header("Location:client_add.php?client_add_error=$client_add_error");
  }
  else {
    // Record inserted, reset session variables and redirect to client.php
    resetForm();
	$_SESSION['sid'] = "";
    header("Location:client.php");
  }
}
?>